﻿using System;
using UnityEngine;

namespace Nomo.Unity.WebRequestModule
{
    /// <summary>
    /// 资源包信息。
    /// </summary>
    public readonly struct AssetBundleInfo : IEquatable<AssetBundleInfo>
    {
        /// <summary>
        /// 键。
        /// </summary>
        public readonly string Key;

        /// <summary>
        /// 统一标识符。
        /// </summary>
        public readonly string Uri;

        /// <summary>
        /// 资源包。
        /// </summary>
        public readonly AssetBundle AssetBundle;

        /// <summary>
        /// 初始化资源包信息的实例。
        /// </summary>
        /// <param name="key">名称。</param>
        /// <param name="uri">统一标识符。</param>
        /// <param name="assetBundle">资源包。</param>
        public AssetBundleInfo(string key, string uri, AssetBundle assetBundle)
        {
            Key         = key;
            Uri         = uri;
            AssetBundle = assetBundle;
        }

        /// <summary>
        /// 判断 <paramref name="a"/> 和 <paramref name="b"/> 是否被视为相等。
        /// </summary>
        /// <param name="a">资源包信息。</param>
        /// <param name="b">另一个资源包信息。</param>
        /// <returns><paramref name="a"/> 和 <paramref name="b"/> 被视为相等吗？</returns>
        public static bool operator ==(AssetBundleInfo a, AssetBundleInfo b)
        {
            return a.Key == b.Key && a.Uri == b.Uri && a.AssetBundle == b.AssetBundle;
        }

        /// <summary>
        /// 判断 <paramref name="a"/> 和 <paramref name="b"/> 是否被视为不相等。
        /// </summary>
        /// <param name="a">资源包信息。</param>
        /// <param name="b">另一个资源包信息。</param>
        /// <returns><paramref name="a"/> 和 <paramref name="b"/> 被视为不相等吗？</returns>
        public static bool operator !=(AssetBundleInfo a, AssetBundleInfo b)
        {
            return a.Key != b.Key || a.Uri != b.Uri || a.AssetBundle != b.AssetBundle;
        }

        /// <inheritdoc />
        public bool Equals(AssetBundleInfo other)
        {
            return this == other;
        }

        /// <inheritdoc />
        public override bool Equals(object obj)
        {
            return obj is AssetBundleInfo other && Equals(other);
        }

        /// <inheritdoc />
        public override int GetHashCode()
        {
            unchecked
            {
                var hashCode = Key                 != null ? Key.GetHashCode() : 0;
                hashCode = (hashCode * 397) ^ (Uri != null ? Uri.GetHashCode() : 0);
                hashCode = (hashCode * 397) ^ (AssetBundle ? AssetBundle.GetHashCode() : 0);
                return hashCode;
            }
        }
    }
}
